package test.combo;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JPanel;

import it.netsphere.nswing.annotation.NSEventMethod;
import it.netsphere.nswing.comp.NSComboBox;
import it.netsphere.nswing.comp.NSComboBoxRenderer;
import it.netsphere.nswing.comp.NSPanel;
import it.netsphere.nswing.event.NSEventCode;
import it.netsphere.nswing.event.NSEventMode;

public class ComboTestPanel extends NSPanel {
	private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory
			.getLogger(ComboTestPanel.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -4343210106471190946L;

	public ComboTestPanel() {
		init();
	}

	private void init() {
		setLayout(new BorderLayout());
		JPanel p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		p2.add(getComboName());
		p2.add(getComboDescr());
		p2.add(getDataButton());
		add(p2, BorderLayout.CENTER);

	}

	@Override
	protected Properties loadProperties(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	private NSComboBox comboName;

	private NSComboBox getComboName() {
		if (comboName == null) {
			comboName = createCombo("comboName", "name",
					NSEventCode.SELECTED_COMBO);
			comboName.setMapping(getSelectionDTO(), "selectedItem");
		}
		return comboName;
	}

	private NSComboBox comboDescr;

	private NSComboBox getComboDescr() {
		if (comboDescr == null) {
			comboDescr = new NSComboBox();
			comboDescr.setRenderer(new NSComboBoxRenderer("descr"));
		}
		return comboDescr;
	}

	private JButton dataButton;

	private JButton getDataButton() {
		if (dataButton == null) {
			dataButton = new JButton();
			dataButton.setText("Add Items");
			dataButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					List<ComboDTO> l = new ArrayList<ComboDTO>();
					for (int c = 0; c < 5; c++) {
						ComboDTO dto = new ComboDTO();
						dto.setName("name-" + c);
						dto.setDescr("descr-dto");
						l.add(dto);
					}
					;

					final ComboDTO selectionItem = ComboDTO.create(
							"---- select ----", "---- select ----");
					// final ComboDTO selectionItem = null;

					getComboName().setData(l, selectionItem);
					getComboDescr().setData(l, selectionItem);
				}
			});
		}

		return dataButton;
	}

	private SelectionDTO selectionDTO;

	private SelectionDTO getSelectionDTO() {
		if (selectionDTO == null) {
			selectionDTO = new SelectionDTO();
		}

		return selectionDTO;
	}

	@NSEventMethod(code=NSEventCode.SELECTED_COMBO, mode=NSEventMode.RESPONSE )
	public void f1() {
		LOG.debug("aaaaaaaaaaa");
	}
}
